//***********************Bismillah-ir-Rahman-ir-Rahim******************************\
//In the Name of Allah
//La ilaha illallah Muhammadur Rasulullah
//Rabbir Hamhuma Kama Rabba Yani Sagheera
#include<bits/stdc++.h>
using namespace std;
#define Alhamdulillah ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define pll pair<ll,ll>
#define vp vector<pair<ll,ll>>
#define pb push_back
#define ff first
#define ss second
#define pi acos(-1.0)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define EPS 1e-9
ll ceilAB(ll a, ll b) { return (a + b - 1) / b; }
ll Pow(ll c, ll d) {return d == 0 ? 1 : c * pow(c, d - 1);}
ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b) {return ((a * b) / gcd(a, b));}
const int mx = 1e6;
int a[mx];
void solve()
{
int i, j, k , n, m, x, y, z, c = 0, cnt = 0;
string s1, s2;
cin >> s1;
cin >> s2;
int len1 = s1.size();
int len2 = s2.size();
if (len1 < len2)
{
sort(s1.begin(), s1.end());
reverse(s1.begin(), s1.end());
cout << s1 << endl;
return;
}
sort(s1.begin(), s1.end());
string ans = s1, s3;
string mx = s1;
for (j = 0; j < len2; j++)
{
for (i = j + 1; i < len1; i++)
{
s3 = s1;
swap(s3[j], s3[i]);
sort(s3.begin() + j + 1, s3.end());
if (s3 <= s2 && s3 > ans)
{
swap(s1[i], s1[j]);
ans = s1;
}
}
}
cout << ans << endl;
}
int main()
{
Alhamdulillah;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
1704C - Virus | 63A - Sinking Ship |
1704B - Luke is a Foodie | 298B - Sail |
239A - Two Bags of Potatoes | 1704E - Count Seconds |
682A - Alyona and Numbers | 44A - Indian Summer |
1133C - Balanced Team | 1704A - Two 0-1 Sequences |
1467A - Wizard of Orz | 1714E - Add Modulo 10 |
1714A - Everyone Loves to Sleep | 764A - Taymyr is calling you |
1714B - Remove Prefix | 1264F - Beautiful Fibonacci Problem |
52A - 123-sequence | 1543A - Exciting Bets |
1714D - Color with Occurrences | 215B - Olympic Medal |
1445A - Array Rearrangment | 1351A - A+B (Trial Problem) |
935B - Fafa and the Gates | 1291A - Even But Not Even |
1269A - Equation | 441A - Valera and Antique Items |
1702C - Train and Queries | 816B - Karen and Coffee |
838D - Airplane Arrangements | 148B - Escape |